/*
 * 版权所有(C)，上海海鼎信息工程股份有限公司，lizhixing，所有权利保留。
 * <p>
 * 项目名：	boot-demo
 * 文件名：	WebSecurityConfig
 * 模块说明：
 * 修改历史：
 * 2022/6/11 - lizhixing - 创建。
 */
package com.lovely602.boot.bootdemo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author lizhixing
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired
  private Environment env;

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    String username = env.getProperty("spring.security.user.name", "guest");
    String password = env.getProperty("spring.security.user.password", "guest");
    auth.inMemoryAuthentication()//
        .passwordEncoder(new BCryptPasswordEncoder())//
        .withUser(username)//
        .password(new BCryptPasswordEncoder().encode(password))//
        .roles("USER");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()//
        .authorizeRequests().antMatchers("/ui/**").hasRole("USER")//
        .antMatchers("/management/**").hasRole("USER")//
        .and().sessionManagement()//
        .and().logout()//
        .and().httpBasic();

  }
}
